package api;

import Dao.Blog;
import Dao.BlogDao;
import Dao.User;
import Dao.UserDao;
import common.Common;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/num")
public class TotalServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String blogId = req.getParameter("blogId");

        User user = Common.checkLoginStatus(req);
        // 没登录的情况
        if(user == null){
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("<script>alert('当前用户未登录')</script>");
            return;
        }
        BlogDao blogDao = new BlogDao();
        if(blogId == null){
            resp.getWriter().write(blogDao.selectTotal(user.getUserId())+"");
        }else {
            Blog blog = blogDao.selectOne(Integer.parseInt(blogId));
            UserDao userDao = new UserDao();
            User author = userDao.selectById(blog.getUserId());
            resp.getWriter().write(blogDao.selectTotal(author.getUserId())+"");
        }
    }
}
